﻿using MMO_MANAGER.DAO;
using MMO_MANAGER.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;

namespace MMO_MANAGER.BUS
{
    class ga_list_bus
    {
        private AccessData dao;
        private SQLiteDataReader dr;
        public ga_list_bus()
        {
            dao = new AccessData();
        }

        public DataTable GetData()
        {
            string sql = "select * from ga_list where active=@act";
            var param = new SQLiteParameter[1];
            param[0] = new SQLiteParameter("@act", DbType.Int16);
            param[0].Value = 1;

            DataTable dt = dao.GetDataSet(sql, param, false).Tables[0];
            return dt;
        }

        public DataTable GetListGAByName(ga_list gl)
        {
            string sql = "select * from ga_list where tenga=@ten and active=@act";
            var param = new SQLiteParameter[2];
            param[0] = new SQLiteParameter("@act", DbType.Int16);
            param[0].Value = 1;
            param[1] = new SQLiteParameter("@ten", DbType.String);
            param[1].Value = gl.tenga;

            DataTable dt = dao.GetDataSet(sql, param, false).Tables[0];
            return dt;
        }

        // - Add : 
        public void Insert(ga_list gl)
        {
            string sql = "Insert Into ga_list (tenga, ghichu, active, ngayedit) " +
                "values (@ten, @ghichu, @act, @ngayedit)";
            var param = new SQLiteParameter[4];
            param[0] = new SQLiteParameter("@ten", DbType.String);
            param[0].Value = gl.tenga;
            param[1] = new SQLiteParameter("@ghichu", DbType.String);
            param[1].Value = gl.ghichu;
            param[2] = new SQLiteParameter("@act", DbType.Int16);
            param[2].Value = gl.active;
            param[3] = new SQLiteParameter("@ngayedit", DbType.DateTime);
            param[3].Value = gl.ngayedit;
            dao.ExecuteNoneQuery(sql, param);
        }

        // - Edit: not edit 'active'
        public void Update(ga_list gl)
        {
            string sql = "Update ga_list SET tenga=@ten, ghichu=@ghichu, ngayedit=@ngayedit where id=@id";
            var param = new SQLiteParameter[4];
            param[0] = new SQLiteParameter("@id", DbType.Int16);
            param[0].Value = gl.id;
            param[1] = new SQLiteParameter("@ten", DbType.String);
            param[1].Value = gl.tenga;
            param[2] = new SQLiteParameter("@ghichu", DbType.String);
            param[2].Value = gl.ghichu;
            param[3] = new SQLiteParameter("@ngayedit", DbType.DateTime);
            param[3].Value = gl.ngayedit;
            dao.ExecuteNoneQuery(sql, param);
        }

        // - Delete: update 'active' = 0
        public void Delete(ga_list gl)
        {
            string sql = "Update ga_list SET active=@act where id=@id";
            var param = new SQLiteParameter[2];
            param[0] = new SQLiteParameter("@id", DbType.Int16);
            param[0].Value = gl.id;
            param[1] = new SQLiteParameter("@act", DbType.Int16);
            param[1].Value = gl.active;
            dao.ExecuteNoneQuery(sql, param);
        }

    }
}
